#ifndef DBWORD_H
#define DBWORD_H

#include "main.h" 





#define WORK_CACHEDIR \
               QString( "%1/.%2/" ).arg(QDir::homePath(),_PROGRAM_SHORT_NAME) 
#define DEFAULTDBFILE \
               QString( "%1wordlist.db" ).arg(WORK_CACHEDIR) 






class DBword : public QObject
{
    Q_OBJECT
//
public:
 DBword()
{
 Load_DB_Connect();
}

void dbquit()
{
    if (db_0.isOpen()) {
    db_0.close();
    }
}

bool ChecktoAppend( const QString w )
{
    bool insertok = false;
    if (db_0.isOpen()) {
    QSqlQuery query(db_0);
    
            int totalin = FindWord(w);
            if (totalin==0 && w.size() < 110) {
            insertok = query.exec(QString("insert into qtgoogle values('%1')").arg(w));
            }
    }
    return insertok;
}

QStringList PaintListWord()
{
    QStringList dbliste;
   if (db_0.isOpen()) {
    QSqlQuery query(db_0);
    QString findword = QString("SELECT word from qtgoogle order by word");
    bool ok = query.exec(findword);
        if (!ok) {
        return dbliste;
        }
        while (query.next()) { 
             dbliste.append(query.value(0).toString());
         }
        
   }
   return dbliste; 
}
int FindWord( const QString w )
{
    int paiv = 0;
    if (db_0.isOpen()) {
    int falsepaiv = 1;
    QSqlQuery query(db_0);
    QString findword = QString("SELECT word from qtgoogle where word ='%1'").arg(w);
    bool ok = query.exec(findword);
    if (!ok) {
    return falsepaiv;
    }
    
       while (query.next()) { 
             paiv++;
             if (paiv > 0) {
             return 1;
             }
            QString se =query.value(0).toString();
         }
      return paiv; 
     } else {
     return 1;   /* not like word if db close.... */
     }
     
}
int SummWord()
{
    int  summ =0;
    if (db_0.isOpen()) {
            QSqlQuery query(db_0);
            QString findword = QString("SELECT count(word)  from qtgoogle");
            bool ok = query.exec(findword);
            if (!ok) {
            return 0;
            }
            while (query.next()) { 
                    summ =query.value(0).toInt();
            }
    }
   return summ;  
}


QString ExportTable_sql()
{
 
    if (db_0.isOpen()) {
    QSqlQuery query(db_0);
    QString findword = QString("SELECT word from qtgoogle order by word");
    bool ok = query.exec(findword);
        
    if (!ok) {
    return QString("No db connection.");
    }
    
    QStringList cvslines; 
    cvslines.append("BEGIN TRANSACTION;");  
    cvslines.append("CREATE TABLE qtgoogle (word TEXT);");
       while (query.next()) { 
              QString se = query.value(0).toString();
           cvslines.append(QString("INSERT INTO qtgoogle values('%1')").arg(se));  
           
         }
               cvslines.append("COMMIT;");  
               const QString contentxt = cvslines.join("\n");
      return contentxt;
         
     }
     
     return QString("No db connection.");
     
}










protected:
    QSqlDatabase db_0;
private:
void Load_DB_Connect()
{
   bool ok;
  ok = fwriteutf8 (QString("%1_index.log").arg( WORK_CACHEDIR) ,"construct.");
  Q_ASSERT(ok);
    
    QStringList drivers = QSqlDatabase::drivers();
    if (!drivers.contains("QSQLITE")) {
       std::cout << "### Load QSQLITE  Driver Fail!." << std::endl; 
        QMessageBox::warning(0, tr("Message"),tr("Load QSQLITE  Driver Fail! Your QT version dont have sqlite3 build."));
       return;      
    }
    
    
  db_0 = QSqlDatabase::addDatabase("QSQLITE","keysearch");
  db_0.setDatabaseName(DEFAULTDBFILE);
  db_0.open();
                      if (!db_0.isOpen()) {
                       QMessageBox::warning(0,_PROGRAM_NAME_,tr("Unable to open db file %1").arg(DEFAULTDBFILE)); 
                   	   return;
                      }

    QString findtableexist = "SELECT name, type, sql FROM sqlite_master WHERE type!='meta' ORDER BY name";
    QString findword = "SELECT word from qtgoogle where word ='Trolltech'";
    QString allword = "SELECT word from qtgoogle order by word";
       QSqlQuery query(db_0);
        ok = query.exec(findtableexist);
        Q_ASSERT(ok);
          if (!db_0.tables().contains("qtgoogle")) {
                //////std::cout << "### make table....." << std::endl; 
               ok =  query.exec("create table qtgoogle (word TEXT)");
          	   Q_ASSERT(ok);
              query.exec("insert into qtgoogle values('Trolltech')");
              query.exec("insert into qtgoogle values('Peter Hohl')");
              query.exec("insert into qtgoogle values('AA_open_text_file_to_grep_words')");
           }
}


signals:
public slots:
};





namespace Ui {
    class BDWordBase: public DBword {};
} // namespace Ui




    
    
#endif // DBWORD_H

